package cur.nature.component.quartz.dao;

import cur.nature.component.quartz.config.CompQuartzDBConfig;
import cur.nature.component.quartz.model.Component_QuartzLogPO;
import cur.nature.framework.core.util.DateUtil;
import cur.nature.framework.core.util.StringUtil;

import cur.nature.framework.mvc.crud.CrudDao;
import cur.nature.component.quartz.model.Component_QuartzLogVO;
import cur.nature.tinyorm.core.bean.PageBean;
import cur.nature.tinyorm.core.template.DefaultJdbcTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 任务调度 Dao
 * 作者：Auto Generator By wujian
 * 生成日期：2024-04-28 18:27:31
 */
@Component
@DependsOn(CompQuartzDBConfig.TEMPLATE)
@ConditionalOnProperty( value = {"cur.component.quartz.enable"})
public class Component_QuartzLogDao extends CrudDao<Component_QuartzLogPO, Component_QuartzLogVO> {
    public Component_QuartzLogDao(@Qualifier(CompQuartzDBConfig.TEMPLATE) DefaultJdbcTemplate template) {
        super(Component_QuartzLogPO.class, Component_QuartzLogVO.class, template);
    }

    public PageBean<Component_QuartzLogVO> list(Component_QuartzLogVO.list_Param param) throws Exception {
        StringBuilder condition = new StringBuilder(" 1=1 ");
//        if(StringUtil.isNotEmpty(param.getKeyword())){
//            condition.append(" and a.code like '"+param.getKeyword()+"%'");
//        }
        if(StringUtil.isNotEmpty(param.getSchedulerId())){
            condition.append(" and a.scheduler_id = "+param.getSchedulerId()+"");
        }
        String sql = "select " + this.cols() + " from " + tableName() + " a " + " where " + condition
                + " order by create_date desc ";
         PageBean<Component_QuartzLogVO> list = queryForPage(sql, param.buildPage(),
                                 null, Component_QuartzLogVO.class);
        return list;
    }

    public int deleteByDate(Long schedulerId, Date date) {
        String cond = " and scheduler_id = " + schedulerId +" "
                + "  and DATE_FORMAT(run_time, '%Y-%m-%d') <> '" + DateUtil.parseDateToStr(date,DateUtil.YYYY_MM_DD) +"'";
        return this.deleteByCond(cond);
    }

}

